package com.dckeji.shiplock.webapp.controller;

import com.dckeji.shiplock.framework.util.StringUtil;
import com.dckeji.shiplock.webapp.pojo.Owner;
import com.dckeji.shiplock.webapp.pojo.UserInfo;
import com.dckeji.shiplock.webapp.service.IOwnerService;
import com.dckeji.shiplock.webapp.util.ReturnMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/7
 * Time: 15:05
 */
public class BaseController {

    protected HttpServletRequest request;

    protected HttpServletResponse response;

    protected HttpSession session;


    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }

    /**
     * 获取Ip地址
     *
     * @return
     */
    protected String getIpAddr() {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    protected boolean valida(Object... para) {
        for (Object obj : para) {
            if (StringUtil.isNull(obj)) {
                return false;
            }
        }
        return true;
    }

    protected boolean isOwner(UserInfo userInfo) {

        if (!StringUtil.isNull(userInfo)) {
            if (userInfo instanceof Oneway) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

//    public UserInfo isLogin(String openid) {
//        String openId = request.getHeader("openId");
//        Object attribute = session.getAttribute(openId);
//        if (null != attribute) {
//            return (UserInfo) attribute;
//        }
//        return null;
//    }
}
